import storage from 'good-storage'
import { observer, inject } from 'mobx-react'
import Config from './config'
import { getQuery, removeQuery } from '../utils/common'
import store from '../stores/index'
import ApiModel from './apiModel/apiModel'
import themes from '../pages/upload/themes'

@inject('UserStore')
@observer
export default class userAuthFun {
  // 检测用户是否授权
  static checkUserAuth(callback) {
    let code = getQuery('code')
    let token = storage.get('token')
    if (token && token !== '') {
      storage.set('userAuthFlag', true)
      callback && callback(true)
    } else if (code && !token) {
      store.UserStore.changeValus(('firstLoading', true))
      this.setOpenId(code)
        .then(() => {
          callback && callback(true, 'reset')
        })
        .catch((e) => {
          storage.set('userAuthFlag', false)
          storage.set('token', '')
          let newUrl = removeQuery('code', window.location.href)

          window.location.href = newUrl
        })
    } else if (!code) {
      storage.set('userAuthFlag', false)
      callback && callback(false)
      this.setWxAuth()
    }
  }
  // 微信授权
  static setWxAuth() {
    let redirect_uri = encodeURIComponent(window.location.href)
    store.UserStore.setNeedUpdateAuth(false)
    window.location.href =
      'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
      Config.appid +
      '&redirect_uri=' +
      redirect_uri +
      '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
  }
  // 设置用户信息access_token和uid
  static setOpenId(code) {
    return new Promise((resolve, reject) => {
      ApiModel.getToken(code)
        // Axios.get(Config.authHost + '?code=' + code)
        .then((res) => {
          if (res && res.code === 0) {
            let token = res.data.token
            storage.set('userAuthFlag', true) // 用户是否已经授权
            store.UserStore.setToken(token)
            store.UserStore.setNeedUpdateAuth(false)
            ApiModel.getUserMsg().then((response) => {
              if (response && response.code === 0) {
                this.checkUserStatus(response)
              }
            })
            resolve()
          } else {
            reject(res)
          }
        })
        .catch((e) => {
          reject()
        })
    })
  }

  // 修改用户的状态
  static checkUserStatus(userMsg) {
    const {
      mode,
      theme,
      theme_child,
      phone,
      school,
      child_name,
      teacher_name,
      age,
      description,
      img_array,
      imgs,
      headimgurl,
    } = userMsg.data
    if (mode == 0 || phone === '') {
      store.UserStore.changeUserState('method')
    } else if (theme == 0) {
      store.UserStore.changeUserState('theme')
    } else if (img_array.length === 0) {
      store.UserStore.changeUserState('uploadMsg')
    } else {
      store.UserStore.changeValus('imgsArray', img_array)
      store.UserStore.changeUserState('uploadSuccess')
    }

    mode && store.UserStore.changeMethod(mode)
    if (theme) {
      themes.map((themeItem, themeIndex) => {
        if (themeItem.key == theme) {
          store.UserStore.changeTheme(theme, themeIndex)
          if (theme_child) {
            themeItem.childrens.map((themeChildItem, themeChildIndex) => {
              if (themeChildItem.key == theme_child) {
                store.UserStore.changeThemeChild(theme_child, themeChildIndex)
              }
            })
          }
        }
      })
    }

    // theme !== '' && store.UserStore.changeTheme(theme)
    // theme_child !== '' && store.UserStore.changeThemeChild(theme_child)
    phone !== '' && store.UserStore.changeValus('phone', phone)
    school !== '' && store.UserStore.changeValus('school', school)
    child_name !== '' && store.UserStore.changeValus('child_name', child_name)
    teacher_name !== '' &&
      store.UserStore.changeValus('teacher_name', teacher_name)
    age && store.UserStore.changeValus('age', age + '岁')
    description !== '' &&
      store.UserStore.changeValus('description', description)
    headimgurl != '' && store.UserStore.changeValus('headimgurl', headimgurl)
    store.UserStore.changeValus(('firstLoading', false))
  }
}
